﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using 刘颜红销售项目管理.Business.DOM ;
using 刘颜红销售项目管理.Business;
using XDesigner.ORM;
using XDesigner.AppFramework;
using XDesigner.AppFramework.DOM;

namespace 刘颜红销售项目管理
{
    public partial class frm人员详细信息 : DevExpress.XtraEditors.XtraForm
    {
        public frm人员详细信息()
        {
            InitializeComponent();
        }

        private Class人员基本信息 _People = null;

        public Class人员基本信息 People
        {
            get { return _People; }
            set { _People = value; }
        }

        public void RefreshView()
        {
            if (this.People == null)
            {
                txtInfo.Text = "";
            }
            else
            {
                txtInfo.Text = "姓名:" + this.People.姓名 + "  手机号:" + this.People.手机号 + " 单位:" + this.People.所属单位名称 + " 电子邮箱:" + this.People.电子信箱 + "   QQ号:" + this.People.QQ号 + " 备注:" + this.People.备注;
            }
            txtPeople.EditValue = _People;
            cbo类型列表.Items.Clear();
            AppCategory cate = MyApplicationFramework.Instance.Categorys[MyConsts.纪念日类型];
            if (cate != null)
            {
                cbo类型列表.Items.Clear();
                cate.FillValueNames(cbo类型列表.Items);
            }
            cate = MyApplicationFramework.Instance.Categorys[MyConsts.费用类型];
            if (cate != null)
            {
                cbo费用类型.Items.Clear();
                cate.FillValueNames(cbo费用类型.Items);
            }

            if ( this.People != null)
            {
                ORMBindingSource bs = ORMBindingSource.CreatePackage(this.People, typeof(Class特殊日期), this.People.特殊纪念日);
                grd特殊纪念日.DataSource = bs;
            }

            
            RefreshGridView( typeof(Class客户访问记录) );
            RefreshGridView(typeof(Class费用信息));

        }

        private void frm人员详细信息_Load(object sender, EventArgs e)
        {
            myTabControl.Dock = DockStyle.Fill;
            myTabControl_SelectedIndexChanged(null, null);
            tvwPeople.RefreshView();
        }
         

        void bs_AddingNew(object sender, AddingNewEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void peopleListEditor_QueryPopUp(object sender, CancelEventArgs e)
        {
            DevExpress.XtraEditors.PopupContainerEdit editor = (DevExpress.XtraEditors.PopupContainerEdit) sender;

            ListContainer list = (ListContainer)editor.EditValue;
            System.Collections.ArrayList list2 = new System.Collections.ArrayList();
            if (list != null)
            {
                list.FillTo(list2);
            }
            tvwPeople.CheckBoxes = true;
            tvwPeople.Controler.CheckedValues = list2.ToArray();
        }

        private void peopleListEditor_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e)
        {
            ListContainer list = new ListContainer();
            foreach (object obj in tvwPeople.Controler.CheckedValues)
            {
                if (obj is Class人员基本信息)
                {
                    list.Add(obj);
                }
            }
            e.Value = list;
        }
         
        private void myTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (myTabControl.SelectedIndex == 0)
            {
                this.peopleListEditor.PopupControl = this.peoplePopupContainerControl;
                this.peopleListEditor2.PopupControl = null;
                myTabControl.TabPages[0].Controls.Add(pnlQuery);
                pnlQuery.Visible = true;
                pnlQuery.Dock = DockStyle.Top;
                pnlQuery.SendToBack();
            }
            else if (myTabControl.SelectedIndex == 1)
            {
                this.peopleListEditor.PopupControl = null;
                this.peopleListEditor2.PopupControl = this.peoplePopupContainerControl;
                myTabControl.TabPages[1].Controls.Add(pnlQuery);
                pnlQuery.Visible = true;
                pnlQuery.Dock = DockStyle.Top;
                pnlQuery.SendToBack();
            }

        }

        private void txtPeople_QueryPopUp(object sender, CancelEventArgs e)
        {
            txtPeople.Properties.PopupControl = peoplePopupContainerControl;
            tvwPeople.CheckBoxes = false;
            tvwPeople.SelectedValue = txtPeople.EditValue;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshGridView(myTabControl.SelectedIndex == 0 ? typeof(Class客户访问记录) : typeof(Class费用信息));
        }
        private void RefreshGridView( Type recordType )
        {
            this.Cursor = Cursors.WaitCursor;
            QueryConditionList conditions = new QueryConditionList();
            List<string> peopleIDs = new List<string>();
            if( txtPeople.EditValue is Class工作单位)
            {
                foreach (Class人员基本信息 people in ((Class工作单位)txtPeople.EditValue).人员列表)
                {
                    peopleIDs.Add(people.RecordID.ToString());
                }
            }
            else if (txtPeople.EditValue is Class人员基本信息)
            {
                peopleIDs.Add(((Class人员基本信息)txtPeople.EditValue).RecordID.ToString());
            }
            if (peopleIDs.Count > 0)
            {
                Array ids = MyApplicationFramework.Instance.ORMEngine.StandardRelationManager.ReadChildIDArray(
                        typeof(Class人员基本信息),
                        peopleIDs.ToArray(),
                        recordType );
                if (ids != null && ids.Length > 0)
                {
                    StringBuilder myStr = new StringBuilder();
                    foreach (object id in ids)
                    {
                        if (myStr.Length > 0)
                        {
                            myStr.Append(",");
                        }
                        myStr.Append(id.ToString());
                    }
                    conditions.AddCondition("RecordID", "in", myStr.ToString());
                }
                else
                {
                    conditions.AddCondition("1", "=", "2");
                }
            }
            if ( chkStartDate.Checked )
            {
                conditions.AddCondition( myTabControl.SelectedIndex == 0 ? "访问时间": "时间", ">=", dtpStart.Value.ToString("yyyyMMdd"));
            }
            if ( chkEndDate.Checked )
            {
                conditions.AddCondition(myTabControl.SelectedIndex == 0 ? "访问时间" : "时间", "<=", dtpEnd.Value.ToString("yyyyMMdd"));
            }

            Array values = MyApplicationFramework.Instance.ORMEngine.ReadInstances( recordType , conditions);
            ORMBindingSource bs = new ORMBindingSource(MyApplicationFramework.Instance.ORMEngine, recordType , values);
            bs.LogItemChange = true;
            if ( recordType.Equals( typeof( Class客户访问记录 )))
            {
                gd访问记录.DataSource = bs;
                bs.AddingNew += delegate(object sender2, AddingNewEventArgs args)
                    {
                        Class客户访问记录 item = (Class客户访问记录)args.NewObject;
                        item.被访人 = new List<Class人员基本信息>();
                        item.访问地点 = this.People.所属单位名称;
                        item.被访人.Add(this.People);
                    };
            }
            else
            {
                gd费用记录.DataSource = bs;
                bs.AddingNew += delegate(object sender2, AddingNewEventArgs args)
                    {
                        Class费用信息 item = (Class费用信息)args.NewObject;
                        item.人员列表 = new List<Class人员基本信息>();
                        item.地址 = this.People.所属单位名称;
                        item.人员列表.Add(this.People);
                    };
            }
            
            this.Cursor = Cursors.Default;
        }

        private void chkStartDate_CheckedChanged(object sender, EventArgs e)
        {
            dtpStart.Enabled = chkStartDate.Checked;
        }

        private void chkEndDate_CheckedChanged(object sender, EventArgs e)
        {
            dtpEnd.Enabled = chkEndDate.Checked;
        }

        private void peopleButtonEditor_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraEditors.BaseEdit editor = (DevExpress.XtraEditors.BaseEdit)sender;
            using (dlgSelectePeople dlg = new dlgSelectePeople())
            {
                dlg.ShowCheckboxes = true;
                dlg.RefreshView();
                ListContainer list = (ListContainer)editor.EditValue;
                System.Collections.ArrayList list2 = new System.Collections.ArrayList();
                if (list != null)
                {
                    list.FillTo(list2);
                }
                dlg.CheckedValues = list2;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    editor.EditValue = new ListContainer(dlg.CheckedValues);
                }
            }
        }

        private void gridView1_GotFocus(object sender, EventArgs e)
        {
            this.peopleListEditor.PopupControl = this.peoplePopupContainerControl;
            this.peopleListEditor2.PopupControl = null;
        }

        private void gridView2_GotFocus(object sender, EventArgs e)
        {
            this.peopleListEditor.PopupControl = null;
            this.peopleListEditor2.PopupControl = this.peoplePopupContainerControl;
        }

        private void tvwPeople_DoubleClick(object sender, EventArgs e)
        {
            if (this.peoplePopupContainerControl.OwnerEdit == txtPeople)
            {
                txtPeople.EditValue = tvwPeople.SelectedValue;
                txtPeople.ClosePopup();
            }
        }
         
    }
}